package com.cjq.system.service.impl;

import com.cjq.common.core.constant.UserConstants;
import com.cjq.common.core.exception.ServiceException;
import com.cjq.common.core.utils.StringUtils;
import com.cjq.system.domain.SysShop;
import com.cjq.system.mapper.SysShopMapper;
import com.cjq.system.mapper.SysUserShopMapper;
import com.cjq.system.service.ISysShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位信息 服务层处理
 *
 * @author ruoyi
 */
@Service
public class SysShopServiceImpl implements ISysShopService
{
    @Autowired
    private SysShopMapper shopMapper;

    @Autowired
    private SysUserShopMapper userShopMapper;

    /**
     * 查询岗位信息集合
     *
     * @param shop 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysShop> selectShopList(SysShop shop)
    {
        return shopMapper.selectShopList(shop);
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysShop> selectShopAll()
    {
        return shopMapper.selectShopAll();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param shopId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysShop selectShopById(Long shopId)
    {
        return shopMapper.selectShopById(shopId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Long> selectShopListByUserId(Long userId)
    {
        return shopMapper.selectShopListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param shop 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkShopNameUnique(SysShop shop)
    {
        Long shopId = StringUtils.isNull(shop.getShopId()) ? -1L : shop.getShopId();
        SysShop info = shopMapper.checkShopNameUnique(shop.getShopName());
        if (StringUtils.isNotNull(info) && info.getShopId().longValue() != shopId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param shop 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkShopCodeUnique(SysShop shop)
    {
        Long shopId = StringUtils.isNull(shop.getShopId()) ? -1L : shop.getShopId();
        SysShop info = shopMapper.checkShopCodeUnique(shop.getShopCode());
        if (StringUtils.isNotNull(info) && info.getShopId().longValue() != shopId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param shopId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserShopById(Long shopId)
    {
        return userShopMapper.countUserShopById(shopId);
    }

    /**
     * 删除岗位信息
     *
     * @param shopId 岗位ID
     * @return 结果
     */
    @Override
    public int deleteShopById(Long shopId)
    {
        return shopMapper.deleteShopById(shopId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param shopIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] shopIds)
    {
        for (Long shopId : shopIds)
        {
            SysShop shop = selectShopById(shopId);
            if (countUserShopById(shopId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", shop.getShopName()));
            }
        }
        return shopMapper.deleteShopByIds(shopIds);
    }

    /**
     * 新增保存岗位信息
     *
     * @param shop 岗位信息
     * @return 结果
     */
    @Override
    public int insertShop(SysShop shop)
    {
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改保存岗位信息
     *
     * @param shop 岗位信息
     * @return 结果
     */
    @Override
    public int updateShop(SysShop shop)
    {
        return shopMapper.updateShop(shop);
    }
}
